package sjtu.ist.warehouse4ws.tool;

import java.io.File;
import java.util.ArrayList;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import sjtu.ist.warehouse4ws.service.PrivilegeService;



import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class ShowDomainModelXmlAction extends ActionSupport {
	PrivilegeService privilegeService;	
	public void setPrivilegeService(PrivilegeService privilegeService) {
		this.privilegeService = privilegeService;
	}
	
	
	String modelXmlJson=null;
	String epcXmlJson=null;
	String functionXmlJson=null;
	String processXmlJson=null;
	String departmentXmlJson=null;
	String wadlXmlJson=null;
	String bpmnXmlJson=null;
	String permissionXmlJson=null;
	String roleXmlJson=null;
	String wsdlXmlJson=null;
	ArrayList<String> list=new ArrayList<String>();
	//public ArrayList<String> showDomainXml(String path){
	public String showDomainXml(){
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();

		String user_id = session.get("user_id").toString();
		String user_company = session.get("user_company").toString();
		int user_type = (Integer)(session.get("user_type"));
		
		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/domain"));
		String path=ServletActionContext.getServletContext().getRealPath("/upload/files/domain/").replaceAll("\\\\", "/");

		File dirfile=new File(path);
		File[] files=dirfile.listFiles();
		for(int i=0;i<files.length;i++){
			String str=files[i].getName();
			if(privilegeService.checkPrivilege(str, user_company, user_type, "read")){
				list.add(str);
			}
		}
		 modelXmlJson=showFunction(list);
		return SUCCESS;
	}
	public String showEpcXml(){
		ActionContext actionContext = ActionContext.getContext();
		Map session = actionContext.getSession();

		String user_id = session.get("user_id").toString();
		String user_company = session.get("user_company").toString();
		int user_type = (Integer)(session.get("user_type"));
		
		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/epc"));
		String path=ServletActionContext.getServletContext().getRealPath("/upload/files/epc/").replaceAll("\\\\", "/");

		File dirfile=new File(path);
		File[] files=dirfile.listFiles();
		for(int i=0;i<files.length;i++){
			String str=files[i].getName();
			if(privilegeService.checkPrivilege(str, user_company, user_type, "read")){
				list.add(str);
			}
		}
		 epcXmlJson=showFunction(list);
		return SUCCESS;
	}
	public String showFunctionXml(){
		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/function"));
		String path=ServletActionContext.getServletContext().getRealPath("/upload/files/function/").replaceAll("\\\\", "/");

		File dirfile=new File(path);
		File[] files=dirfile.listFiles();
		for(int i=0;i<files.length;i++){
			String str=files[i].getName();
			list.add(str);	
		}
		 functionXmlJson=showFunction(list);
		return SUCCESS;
	}
	public String showProcessXml(){
		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/process"));
		String path=ServletActionContext.getServletContext().getRealPath("/upload/files/process/").replaceAll("\\\\", "/");

		File dirfile=new File(path);
		File[] files=dirfile.listFiles();
		for(int i=0;i<files.length;i++){
			String str=files[i].getName();
			list.add(str);	
		}
		 processXmlJson=showFunction(list);
		return SUCCESS;
	}
	public String showDepartmentXml(){
		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/department"));
		String path=ServletActionContext.getServletContext().getRealPath("/upload/files/department/").replaceAll("\\\\", "/");

		File dirfile=new File(path);
		File[] files=dirfile.listFiles();
		for(int i=0;i<files.length;i++){
			String str=files[i].getName();
			list.add(str);	
		}
		 departmentXmlJson=showFunction(list);
		return SUCCESS;
	}
	
	public String showPermissionXml(){
		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/permission"));
		String path=ServletActionContext.getServletContext().getRealPath("/upload/files/permission/").replaceAll("\\\\", "/");

		File dirfile=new File(path);
		File[] files=dirfile.listFiles();
		for(int i=0;i<files.length;i++){
			String str=files[i].getName();
			list.add(str);	
		}
		 permissionXmlJson=showFunction(list);
		return SUCCESS;
	}
	
	public String showRoleXml(){
		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/role"));
		String path=ServletActionContext.getServletContext().getRealPath("/upload/files/role/").replaceAll("\\\\", "/");

		File dirfile=new File(path);
		File[] files=dirfile.listFiles();
		for(int i=0;i<files.length;i++){
			String str=files[i].getName();
			list.add(str);	
		}
		 roleXmlJson=showFunction(list);
		return SUCCESS;
	}
	
	
	public String showWadlXml(){
		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/wadl"));
		String path=ServletActionContext.getServletContext().getRealPath("/upload/files/wadl/").replaceAll("\\\\", "/");

		File dirfile=new File(path);
		File[] files=dirfile.listFiles();
		for(int i=0;i<files.length;i++){
			String str=files[i].getName();
			list.add(str);	
		}
		wadlXmlJson=showFunction(list);
		return SUCCESS;
	}
	public String showBpmnXml(){
		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/bpmn"));
		String path=ServletActionContext.getServletContext().getRealPath("/upload/files/bpmn/").replaceAll("\\\\", "/");

		File dirfile=new File(path);
		File[] files=dirfile.listFiles();
		for(int i=0;i<files.length;i++){
			String str=files[i].getName();
			list.add(str);	
		}
		bpmnXmlJson=showFunction(list);
		return SUCCESS;
	}
	public String showWsdlXml(){
		MyUtils.mkDirectory(ServletActionContext.getServletContext().getRealPath("/upload/files/wsdl"));
		String path=ServletActionContext.getServletContext().getRealPath("/upload/files/wsdl/").replaceAll("\\\\", "/");

		File dirfile=new File(path);
		File[] files=dirfile.listFiles();
		for(int i=0;i<files.length;i++){
			String str=files[i].getName();
			list.add(str);	
		}
		wsdlXmlJson=showFunction(list);
		return SUCCESS;
	}
	
	public String showFunction(ArrayList<String> list){
		StringBuffer processjson = new StringBuffer();
		processjson.append("[");
		for(String function:list){
			processjson.append("{text:'");
			processjson.append(function);
			processjson.append("'");
			processjson.append(",leaf:true },");
			
		}
		processjson.deleteCharAt(processjson.length()-1);
		processjson.append("]");
	
		return processjson.toString();
	}
	public String getModelXmlJson() {
		return modelXmlJson;
	}
	public void setModelXmlJson(String modelXmlJson) {
		this.modelXmlJson = modelXmlJson;
	}
	public ArrayList<String> getList() {
		return list;
	}
	public void setList(ArrayList<String> list) {
		this.list = list;
	}
	public String getEpcXmlJson() {
		return epcXmlJson;
	}
	public void setEpcXmlJson(String epcXmlJson) {
		this.epcXmlJson = epcXmlJson;
	}
	public String getFunctionXmlJson() {
		return functionXmlJson;
	}
	public void setFunctionXmlJson(String functionXmlJson) {
		this.functionXmlJson = functionXmlJson;
	}
	public String getProcessXmlJson() {
		return processXmlJson;
	}
	public void setProcessXmlJson(String processXmlJson) {
		this.processXmlJson = processXmlJson;
	}
	public String getDepartmentXmlJson() {
		return departmentXmlJson;
	}
	public void setDepartmentXmlJson(String departmentXmlJson) {
		this.departmentXmlJson = departmentXmlJson;
	}
	
	public String getPermissionXmlJson() {
		return permissionXmlJson;
	}
	public void setPermissionXmlJson(String permissionXmlJson) {
		this.permissionXmlJson = permissionXmlJson;
	}
	
	public String getRoleXmlJson() {
		return roleXmlJson;
	}
	public void setRoleXmlJson(String roleXmlJson) {
		this.roleXmlJson = roleXmlJson;
	}
	
	public String getWadlXmlJson() {
		return wadlXmlJson;
	}
	public void setWadlXmlJson(String wadlXmlJson) {
		this.wadlXmlJson = wadlXmlJson;
	}
	public String getBpmnXmlJson() {
		return bpmnXmlJson;
	}
	public void setBpmnXmlJson(String bpmnXmlJson) {
		this.bpmnXmlJson = bpmnXmlJson;
	}
	public String getWsdlXmlJson() {
		return wsdlXmlJson;
	}
	public void setWsdlXmlJson(String wsdlXmlJson) {
		this.wsdlXmlJson = wsdlXmlJson;
	}
	

	
}
